package com.net.service.website;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.net.controller.model.ResponseModel;
import com.net.dao.UserDAO;
import com.net.dao.WebsiteDAO;
import com.net.dao.WebsiteTypeDAO;
import com.net.model.Website;
import com.net.model.WebsiteType;
import com.net.service.vo.ServiceCode;
import com.net.util.ResultHelper;

public class WebsiteServiceImpl implements WebsiteService {
	private WebsiteDAO websiteDAO;
	private WebsiteTypeDAO websiteTypeDAO;

	@Autowired
	public void setWebsiteDAO(WebsiteDAO websiteDAO) {
		this.websiteDAO = websiteDAO;
	}

	@Autowired
	public void setWebsiteTypeDAO(WebsiteTypeDAO websiteTypeDAO) {
		this.websiteTypeDAO = websiteTypeDAO;
	}

	@Override
	public List<Website> listAll() {
		return websiteDAO.getAll();
	}

	@Override
	public ResponseModel editWebsite(int id, String name, String domain, String type) {
		WebsiteType websiteType;
		websiteType = websiteTypeDAO.getByName(type);
		if (websiteType == null) {
			return ResultHelper.createResult(ServiceCode.FAIL, "类型输入错误");
		} else {
			Website website = new Website();
			website.setId(id);
			website.setName(name);
			website.setDomain(domain);
			website.setType(websiteType);
			websiteDAO.update(website);
			return ResultHelper.createResult(ServiceCode.SUCCESS, "更新成功！");
		}

	}

	@Override
	public boolean delWebsite(int id) {
		websiteDAO.del(id);
		return true;
	}

	@Override
	public ResponseModel addWebsite(String name, String domain, String type) {
		WebsiteType websiteType;
		websiteType = websiteTypeDAO.getByName(type);
		if (websiteType == null) {
			return ResultHelper.createResult(ServiceCode.FAIL, "类型输入错误");
		} else {
			Website website = new Website();
			website.setName(name);
			website.setDomain(domain);
			website.setType(websiteType);
			websiteDAO.add(website);
			return ResultHelper.createResult(ServiceCode.SUCCESS, "新增成功");
		}
	}

	@Override
	public ResponseModel addWebsiteType(String name, String note) {
		WebsiteType websiteType = websiteTypeDAO.getByName(name);
		if (websiteType != null) {
			return ResultHelper.createResult(ServiceCode.FAIL, "类型已经存在，不能重复添加");
		} else {
			websiteType = new WebsiteType();
			websiteType.setName(name);
			websiteType.setNote(note);
			if (websiteTypeDAO.add(websiteType) == 1) {
				return ResultHelper.createResult(ServiceCode.SUCCESS, "添加成功");
			}
		}
		return ResultHelper.createResult(ServiceCode.FAIL, "服务器异常，类型添加失败");
	}

	@Override
	public ResponseModel editWebsiteType(int id, String name, String note) {
		WebsiteType websiteType = websiteTypeDAO.getById(id);
		if (websiteType == null) {
			return ResultHelper.createResult(ServiceCode.FAIL, "参数错误,编辑失败");
		}
		websiteType = websiteTypeDAO.getByName(name);
		if (websiteType != null && id != websiteType.getId()) {
			return ResultHelper.createResult(ServiceCode.FAIL, "类型重复,编辑失败");
		}
		websiteType = new WebsiteType();
		websiteType.setId(id);
		if (name != null && !"".equals(name)) {
			websiteType.setName(name);
		}

		if (note != null && !"".equals(note)) {
			websiteType.setNote(note);
		}
		if (websiteTypeDAO.edit(websiteType) == 1) {
			return ResultHelper.createResult(ServiceCode.SUCCESS, "编辑成功");
		}

		return ResultHelper.createResult(ServiceCode.FAIL, "服务器异常，编辑失败");
	}

	@Override
	public boolean delWebsiteType(int id) {
		websiteTypeDAO.del(id);
		return true;
	}

	@Override
	public List<WebsiteType> getAllType() {
		// TODO Auto-generated method stub
		return websiteTypeDAO.getAll();
	}

}
